int distWall =10;
boolean gameOver;
boolean specialStage;
public class Jogo extends Janela{
  private float distWall,quinaSize;
  private color corIniBola;
  private int trans;
  private boolean pause;
  //cria os objetos
  private PImage fundo, fundo2;
  private Level level;
//  Musica musica;
//  Som som;
  private Ball bola;
  private Paddle paddleL,paddleR;
  private Controle p1,p2;
  private PowerBall pb;
  private Colisao colisao;
  private Placar gamePlacar;
  private Player player1, player2;
  private Placar placarP1, placarP2;
  private Item item;
  private Rastro rastro;
  private Ia ia;
  //estancia as variáveis e os objetos
  Jogo(){//Musica musica_, Som som_){
    super();
     font = loadFont("fontes//ComicSansMS-48.vlw");
    //quina
    quinaSize=5;
    distWall=10;
 
    fundo = loadImage("imagens//jogo.jpg");
    fundo2= loadImage("imagens//jogo2.jpg");
//    musica = musica_;
//    som = som_;
    
    //player1
    player1 = new Player();
    player1.nome = "Player1";
    player1.cor = color(49,66,242);
    player1.vida=3;
    
    //player2
    player2 = new Player();
    player2.nome = "Player2";
    player2.cor = color(111,252,105);
    
    //bola
    bola = new Ball();
    bola.size=10;
    bola.x=width/2;
    bola.y=height/2;
    bola.velX=1;
    corIniBola = color(255);
    bola.cor(corIniBola);
   
    //raquete esquerda
    paddleL = new Paddle();
    paddleL.h=50;
    paddleL.w=5;
    paddleL.limitUp=paddleL.h;
    paddleL.limitDown=height;
    paddleL.cor=color(255);
    paddleL.x=distWall;
    paddleL.cor = player1.cor;
    
    //raquete direita
    paddleR = new Paddle();
    paddleR.h=50;
    paddleR.w=5;
    paddleR.limitUp=paddleR.h;
    paddleR.limitDown=height;
    paddleR.cor=color(player2.cor);
    paddleR.x=width-distWall;
    
    //level
    level = new Level(bola);
    
    //rastro
    rastro = new Rastro(bola,color(255,3,2,20)); 
    
    //PowerBall
    pb = new PowerBall(bola, rastro);//, som);
    
    rastro.setPowerBall(pb);
    
    //colisao da bola com a raquete
    colisao = new Colisao(pb);//, som);
   
    
    //controle
    p1 = new Controle(paddleL, distWall);
    p2 = new Controle(paddleR, distWall);
    
    //placar
    gamePlacar = new Placar();
    placarP1 = new Placar();
    placarP2 = new Placar();
    placarP2.x=width-placarP2.w;
    
    item = new Item("PB",color(255,247,0),bola);
    ia = new Ia(paddleR,bola,pb);
  }// fim construtor

  void update(){
    if(!pause){
      //comeca a musica
//      musica.start();
      
      if(trans <= 0)
        //movimenta a bola
        bola.move();
      
      //controle das raquetes
      p1.teclado(38, 40, 17);
      
      //p2.teclado('w','s');
      ia.start();
    }
    
    //level
    level.level();
    
    //colisao com a raquete
    colisao.colisaoVertical(bola, paddleL);
    colisao.colisaoVertical(bola, paddleR);
    
    if (pb.power < 40) pb.power = colisao.power;//atualiza o power
    
    //cria parede de cima e de baixo
    paredeHorizontal(bola);
    
    //verifica qual dos jogadores perdeu
    perde();
    
    //perde vida
    perdeVida(player1);
    perdeVida(player2);
    
    //desenha todas as coisas da tela
    show();
    
    //se apertar ESC entra no MENU
    entraMenu();
    
    gameOver(player1);
    
    if (specialStage) specialStage();
    
  }//fim start()

  void show(){
     background(fundo);
     noStroke();
     bola.desenha();
     paddleL.desenha();
     paddleR.desenha();
     quina();
     gamePlacar.game(pb.power-10,level.level);
     placarP1.player(player1);
     //placarP2.player(player2);
     pause();
     //teste();
     //item.start();
     if(pause==false && trans<=0) pb.start();
     if(trans >0) liga=false; 
//     som.setPan(som.s3, bola);
     mudaLevel();
       
   }
  void setPlayerName(String s){
    player1.nome = s;
  }
  void paredeHorizontal (Ball b){
    if(b.y + b.size > height || b.y < b.size + gamePlacar.h) b.dY *= -1;
  }
  
  void perde(){    
    if(bola.x < -width/2){// player 1 perde
      player1.perde = true;
      pb.power = 10;
      recomeca();
    }
    
    if(bola.x > width + width/2){//player 2 perde
      player2.perde = true;
      recomeca();
    }
  }
  
  void mudaLevel(){
    if (trans > 0){  
      trans -= 2;
      fill(255, 255, 255, trans);
      text("Level " + level.level + "\nSTART", width/2 - 5*4.5, height/3);
    }
  }
  
  void recomeca(){
    bola.dX *= -1;
    bola.velY = 0;
    bola.x = width/2 - bola.size + 5;
    bola.y = height/2 + bola.size + 3;
    bola.cor = corIniBola;
    colisao.power = 0;
    liga = false;
    if(player2.perde) level.level++;
    trans = 255;
  }
    
  
  void perdeVida(Player p){
    if (p.perde){
      p.vida--;
      p.perde = false;
    }
  }  
  
  void quina(){
    fill(255, 255, 255, 100);
    ellipse(distWall, distWall + gamePlacar.h, quinaSize, quinaSize);
    ellipse(distWall, height - distWall, quinaSize, quinaSize);
    ellipse(width - distWall, height - distWall, quinaSize, quinaSize);
    ellipse(width - distWall, distWall + gamePlacar.h, quinaSize, quinaSize);
  }
  
  void entraMenu(){
    if  (keyPressed && keyCode == 112)
      setActive(opcoes);
  }
  
  void pause(){
    if(pause){
       fill(0, 0, 0, 100);
       rectMode(RADIUS);
       rect(0, 0, width, height);
       fill(255);
       stroke(0);
       textFont(font, 15);
       text("PAUSE", width/2 - 5*3, height/2);
       text("Precione F1 para entra em opções", width/2 - 32*3, height/3);
    }
    if(keyPressed){
      if (keyCode == 112)
        pause = true;
      if (keyCode == 16){
        delay(200);
        pause = !pause;
      }
    }
  }//fim pause()  
  
  void gameOver(Player p){
    
    if(p.vida == 0 || level.level > 6){
      String mensagem = "";
      if(p.vida == 0){
        gameOver = true;
        mensagem = "GAME OVER";
      }
      if(level.level > 1){

        if(specialStage) mensagem="Thanks for Playing!";  

        if(!specialStage && !gameOver) mensagem = "Congratulation!!\nSpecial Level Unlocked";
      }
      pause = true;
//      musica.stop();
      fill(0);
      rect(0, 0, width, height);
      fill(255);
      text(mensagem, width/2 - 9*4.5, height/2);
      
      if(keyPressed || mousePressed){
        if(!specialStage && !gameOver){
          db1.addHiscoreByPName(p.nome, level.level);
          setActive(hiscore);
          specialStage = true;
        }

        if(gameOver){
          db1.addHiscoreByPName(p.nome, level.level);
          setActive(hiscore);
        }

        if(!gameOver && specialStage){
          db1.addHiscoreByPName(p.nome, level.level);
          setActive(hiscore);
        }

        p.vida = 3;
        level.level = 1;
      }
    }
  }
  
  
  void specialStage(){
    fundo=fundo2;
    pb.power=40;
  }
  
  
  void teste(){
    if(mousePressed){
       bola.x=mouseX;
       bola.y=mouseY;
       
      // bola.velX=-1;
       //bola.velY=0;
       liga=true;
//       som.play(3);
       pb.power=40;
    }
    if(keyPressed){
      if(key=='+'){
        delay(100);
        bola.velX++;
      }
      if(key=='-'){
        bola.velX--;
        delay(100);
      }
      text(keyCode, 300,200);
    }
    fill(255,70,3);
    textFont(font,12);
    //text("bola.velY: "+bola.velY+"  bola.velX: "+bola.velX,300,300);
   // text("bola.y - paddleL.y: "+Math.abs(bola.y - paddleL.y),300,280);
    //text("paddleL.y: "+paddleL.y,300,260);
    //text("bola.x: "+bola.x+"  bola.y: "+bola.y,300,240);
    //text("p1.c:"+p1.c+"  p2.c:"+p2.c,300,180);  
  }// termina teste()
}//fim da classe
